<template>
  <div class="c-ph24 c-flex-row c-aligni-center" v-if="showCoupon || showDiscount || showChain || showPoints">
    <div class="c-w0 c-flex-grow1 c-flex-row c-aligni-center c-text-hidden c-flexw-wrap c-lh40 c-hh40">
      <template v-if="showCoupon">
        <div v-for="(item,index) in list" :key="index" class="bg-FF300C-10 c-fc-xmlred c-br10 c-fs20 c-lh40 c-mr16 c-ph10 c-hh40 c-lh40">
          {{showCouponText(item)}}
          <!-- {{item.amount * 1 > 0 ? ('满' + item.amount.split('.')[0] +'减'+item.price * 1) : (item.type == 5 ? (item.price * 1 +'折无门槛') : (item.price * 1+'元无门槛'))}} -->
        </div>
      </template>
      <template v-if="showDiscount && optimal">
        <div class="bg-FF300C-10 c-fc-xmlred c-br10 c-fs20 c-lh40 c-mr16 c-ph10 c-hh40">
          {{idName}}
          <span v-if="vipDiscountType == 1 && idDiscount != 100 && idDiscount != 0">享{{Number(idDiscount / 10).toFixed(2)}}折</span>
          <span v-else-if="vipDiscountType == 2 && idPrice > 0 && idPrice != originPrice">减{{'￥' | iosPriceFilter}}{{Number(originPrice - idPrice).toFixed(2)}}</span>
          <span v-else-if="idDiscount == 0 || idPrice == 0">免费</span>
        </div>
      </template>
      <div v-if="showChain" class="bg-FF300C-10 c-fc-xmlred c-br10 c-fs20 c-lh40 c-mr16 c-ph10 c-hh40">
        <template v-if="optimal">
          <span v-if="reOrderBonus > 0 && reOrderBonus < Number(originPrice)">复购价<span v-if="Number(reOrderBonus) > 0">{{'￥' | iosPriceFilter}}</span>{{Number(reOrderBonus) == 0 ? "免费" : Number(reOrderBonus).toFixed(2)}}</span>
          <span v-else-if="chainOrderBonus > 0 && chainOrderBonus < Number(originPrice)" class="c-mr16">优惠价<span v-if="Number(chainOrderBonus) > 0">{{'￥' | iosPriceFilter}}</span>{{Number(chainOrderBonus) == 0 ? "免费" : Number(chainOrderBonus).toFixed(2)}}</span>
        </template>
        <template v-else>
          <span v-if="chainOrderBonus > 0 && chainOrderBonus < Number(originPrice)" class="c-mr16">优惠价<span v-if="Number(chainOrderBonus) > 0">{{'￥' | iosPriceFilter}}</span>{{Number(chainOrderBonus) == 0 ? "免费" : Number(chainOrderBonus).toFixed(2)}}</span>
          <span v-if="reOrderBonus > 0 && reOrderBonus < Number(originPrice)">复购价<span v-if="Number(reOrderBonus) > 0">{{'￥' | iosPriceFilter}}</span>{{Number(reOrderBonus) == 0 ? "免费" : Number(reOrderBonus).toFixed(2)}}</span>
        </template>
      </div>
      <div v-if="showPoints" class="bg-FF300C-10 c-fc-xmlred c-br10 c-fs20 c-lh40 c-mr16 c-ph10 c-hh40">
        {{companyAuth.integrateName1}}可抵 {{'￥' | iosPriceFilter}}{{deductiblePoints}}
      </div>
    </div>
    <div v-if="showCoupon" @click="clickShowCoupon" class="c-ww88 c-hh36 c-bg-xmlred c-br18 c-flex-row c-flex-center">
      <div class="c-fs20 c-fc-white">领券</div>
      <div class="c-flex-row c-flex-center c-ww20 c-hh20 c-br10 c-bg-white c-ml6">
        <span class="iconfont c-fs10 c-fc-xmlred">&#xe60e;</span>
      </div>
    </div>
  </div>
</template>

<script>
import { utilJs } from "@/utils/common.js";
export default {
  name: 'DiscountInfo',
  props: {
    couponList: {
      type: Array,
      default: () => []
    },
    isMarketing: { // 处于营销活动(拼团、限时折扣、好友助力、砍价)中
      type: Boolean,
      default: false
    },
    chainInfo: {
      type: Object,
      default: () => null
    },
    dataInfo: {
      type: Object,
      default: () => null
    }
  },
  data() {
    return {
      companyAuth: JSON.parse(localStorage.getItem("companyAuth")),
      vipType: localStorage.getItem("vipType" + localStorage.getItem("userId")) ? localStorage.getItem("vipType" + localStorage.getItem("userId")) : 1,
      list: [],
      discountPrice: 0,
      chainPrice: 0,
      ckFrom: global.ckFrom
    }
  },
  watch: {
    dataInfo: {
      handler(newVal) {
        const dataInfo = JSON.parse(JSON.stringify(newVal));
        const vipType = this.vipType;
        if (dataInfo) {
          this.vipDiscount = Number(dataInfo.vipDiscount);
          this.svipDiscount = Number(dataInfo.svipDiscount);
          this.vipDiscountPrice = Number(dataInfo.vipDiscountPrice);
          this.svipDiscountPrice = Number(dataInfo.svipDiscountPrice);
          this.originPrice = Number(dataInfo.price);
          this.deductiblePoints = Number(dataInfo.deductiblePoints);
          // 商城跟其他模块的关于代理商折扣vipDiscountType判断不一样 其他模块的commonProdInfo.agentDiscount都是百分比
          this.vipDiscountType = (dataInfo.prodType != this.ckFrom.shopping && (vipType == 3 || vipType > 4) && dataInfo.commonProdInfo && dataInfo.commonProdInfo.agentDiscount < 100) ? 1 : (dataInfo.vipDiscountType ? Number(dataInfo.vipDiscountType) : 1);
          let custNameMap = utilJs.getCustNameMap();
          const agentDiscount = dataInfo.prodType != this.ckFrom.shopping ?
                                (dataInfo.commonProdInfo && dataInfo.commonProdInfo.agentDiscount < 100 ? dataInfo.commonProdInfo.agentDiscount : 100) :
                                (dataInfo.commonProdInfo && dataInfo.commonProdInfo.agentDiscount ? dataInfo.commonProdInfo.agentDiscount : this.vipDiscountType == 1 ? 100 : dataInfo.price);
          const svipDiscount = dataInfo.svipDiscount < 100 ? dataInfo.svipDiscount : 100;
          const vipDiscount = dataInfo.vipDiscount < 100 ? dataInfo.vipDiscount : 100;
          if (this.optimal && (!this.isMarketing || (!this.isInMarketTwoPrice && this.isMarketing))) {
            // optimalPriceType：1 产品本身价格 2 vip价格 3 svip价格 4 服务商 101 限时购 102 拼团 112 砍价  10 链动会员 20 链动代理商 99 链动复购
            const optimal = dataInfo.optimal;
            this.idName = optimal.optimalPriceType == 2 ? 'VIP' : optimal.optimalPriceType == 3 ? 'SVIP' : optimal.optimalPriceType == 4 ? custNameMap[vipType] : '';
            this.idPrice = optimal.optimalPrice;
            this.idDiscount = optimal.optimalPriceType == 2 ? vipDiscount : optimal.optimalPriceType == 3 ? svipDiscount : optimal.optimalPriceType == 4 ? agentDiscount : 100;
          } else {
            custNameMap[1] = 'VIP';
            this.idName = custNameMap[vipType];
            // 商城跟其他模块字段不一致
            if (dataInfo.prodType == this.ckFrom.shopping) {
              this.idPrice = dataInfo.vipDiscountType == 2 ? (
                (vipType == 4 && Number(dataInfo.svipDiscount) < Number(dataInfo.price))
                  ? Number(dataInfo.svipDiscount)
                  :
                (vipType == 2 && Number(dataInfo.vipDiscount) < Number(dataInfo.price))
                  ? Number(dataInfo.vipDiscount)
                  :
                ((vipType == 3 || vipType > 4) && Number(dataInfo.commonProdInfo.agentDiscount) < Number(dataInfo.price))
                  ? Number(dataInfo.commonProdInfo.agentDiscount) : Number(dataInfo.price)
              ) : Number(dataInfo.price);
            } else {
              this.idPrice = dataInfo.vipDiscountType == 2 ? (
                (vipType == 4 && Number(dataInfo.svipDiscountPrice) < Number(dataInfo.price))
                  ? Number(dataInfo.svipDiscountPrice)
                  :
                (vipType == 2 && Number(dataInfo.vipDiscountPrice) < Number(dataInfo.price))
                  ? Number(dataInfo.vipDiscountPrice)
                  : Number(dataInfo.price)
              ) : Number(dataInfo.price);
            }
          }
          this.idDiscount = (vipType == 3 || vipType > 4) && agentDiscount < 100 ? agentDiscount :
                    vipType == 4 && svipDiscount < 100 ? svipDiscount :
                    ((vipType == 2 && dataInfo.optimal) && vipDiscount < 100) ? vipDiscount : 100;
          this.discountPrice = this.vipDiscountType == 1 ? Number(this.dataInfo.price * this.idDiscount / 100) : this.idPrice;
          this.chainPrice = this.chainInfo && this.chainInfo.isChainProd ?
                            (this.chainInfo.enableReorderDiscount == 1 && this.chainInfo.isReorder ? this.chainInfo.reorderBonus : this.chainInfo.enableOrderDiscount == 1 ? this.chainInfo.orderBonus : this.dataInfo.price) :
                            this.dataInfo.price;
        }
      },
      immediate: true,
      deep: true
    },
    chainInfo: {
      handler(newVal) {
        const chainInfo = JSON.parse(JSON.stringify(newVal));
        if (chainInfo) {
          this.isChain = chainInfo.isChainProd;
          this.chainOrderBonus = chainInfo.enableOrderDiscount == 1 ? chainInfo.orderBonus : 0;
          this.reOrderBonus = chainInfo.enableReorderDiscount == 1 && chainInfo.isReorder ? chainInfo.reorderBonus : 0;
          this.chainIdName = chainInfo.enableReorderDiscount == 1 && chainInfo.isReorder ?
                  '复购价' : chainInfo.userType == 1 ? '普通用户' : chainInfo.userType2Name[chainInfo.userType];
        }
      },
      immediate: true,
      deep: true
    },
    couponList: {
      handler(newList, oldList) {
        if (newList) {
          this.list = newList.slice(0, 1);
        }
      },
      immediate: true,
    }
  },
  computed: {
    // 营销活动中存在两种最低价格（最低价格不是营销价格）
    isInMarketTwoPrice() {
      if (!this.dataInfo) return false;
      if (this.dataInfo.prodType == this.ckFrom.shopping) {
        return this.dataInfo.isInMarketTwoPrice || false
      }
      return this.optimal && !((this.dataInfo.sales && this.optimal.optimalPriceType != 101) || (this.dataInfo.bargain && ((this.dataInfo.bargain.status == 1 &&
            this.dataInfo.bargain.isPreheat == 1) || this.dataInfo.bargain.status == 2)) || (this.dataInfo.collage && (this.dataInfo.collage.status == 2 || this.dataInfo.collage.status == 1)) || (this.dataInfo.isFriendHelper)) ? false : true
    },
    optimal() {
      return this.dataInfo ? this.dataInfo.optimal : null;
    },
    // 折扣判断显示条件
    showDiscount() {
      if (!this.dataInfo) return false;
      let showDisPrice = this.optimal && [2, 3, 4].includes(this.optimal.optimalPriceType) && !this.isMarketing;
      if (this.isInMarketTwoPrice) {
        this.discountPrice = this.vipDiscountType == 1 ? Number(this.dataInfo.price * this.idDiscount / 100) : this.idPrice;
        this.chainPrice = this.chainInfo && this.chainInfo.isChainProd ?
                        (this.chainInfo.enableReorderDiscount == 1 && this.chainInfo.isReorder ? this.chainInfo.reorderBonus : this.chainInfo.enableOrderDiscount == 1 ? this.chainInfo.orderBonus : this.dataInfo.price) :
                        this.dataInfo.price;
        showDisPrice = (this.chainInfo && this.chainInfo.isChainProd) ? this.discountPrice * 1 < this.chainPrice * 1 : this.discountPrice * 1 < this.dataInfo.price * 1;
      }
      return !this.$isWxAppIos() && showDisPrice
    },
    // 优惠券判断显示条件
    showCoupon() {
      return this.list && this.list.length > 0
    },
    // 积分判断显示条件
    showPoints() {
      if (!this.dataInfo) return false;
      // 推荐好友 营销有直接购买入口的展示，但是限时折扣最低价格不是活动价的展示
      return this.companyAuth.enablePoints == 1 && this.deductiblePoints > 0 &&
            (
              (this.dataInfo.prodType != this.ckFrom.shopping && ((this.dataInfo.sales && this.optimal && this.optimal.optimalPriceType != 101) || !this.dataInfo.sales)) ||
              (this.dataInfo.prodType == this.ckFrom.shopping && this.optimal && this.optimal.optimalPriceType != 101)
            )
    },
    // 链动判断显示条件
    showChain() {
      if (!this.chainInfo) return false;
      let showChainPrice = this.optimal && [10, 20, 99].includes(this.optimal.optimalPriceType) && !this.isMarketing;
      if (this.isInMarketTwoPrice) {
        this.discountPrice = this.vipDiscountType == 1 ? Number(this.dataInfo.price * this.idDiscount / 100) : this.idPrice;
        this.chainPrice = this.chainInfo && this.chainInfo.isChainProd ?
                        (this.chainInfo.enableReorderDiscount == 1 && this.chainInfo.isReorder ? this.chainInfo.reorderBonus : this.chainInfo.enableOrderDiscount == 1 ? this.chainInfo.orderBonus : this.dataInfo.price) :
                        this.dataInfo.price;
        showChainPrice = (this.chainInfo && this.chainInfo.isChainProd) ? this.chainPrice * 1 <= this.discountPrice * 1 : this.chainPrice * 1 <= this.dataInfo.price * 1;
      }
      return !this.$isWxAppIos() && showChainPrice && this.chainIdName && ((this.chainOrderBonus > 0 && this.chainOrderBonus < Number(this.originPrice)) || (this.reOrderBonus > 0 && this.reOrderBonus < Number(this.originPrice)))
    },
  },

  methods: {
    clickShowCoupon() {
      this.$emit('clickShowCoupon');
    },
    showCouponText(cItem) {
      let couponText = '';
      if (cItem.type == 1) {
        couponText = cItem.price * 1 + '元无门槛'
      }
      if (cItem.type == 3) {
        if (cItem.subType == 1) {
          couponText = cItem.price * 1 + '元无门槛'
        } else if (cItem.subType == 2) {
          couponText = ('满' + cItem.amount + '减' + cItem.price * 1)
        }
      }
      if (cItem.type == 5) {
        if (cItem.subType == 1) {
          couponText = cItem.price * 1 + '折无门槛'
        } else if (cItem.subType == 2) {
          couponText = ('满' + cItem.amount + '打' + cItem.price * 1 + '折')
        }
      }
      return couponText;
    }
  },
  mounted() {
  },
  destroyed() {
  }
}
</script>

<style scoped>
.bg-FF300C-10 {
  background-image: linear-gradient(270deg, #FEF2EB 0%, #FCE9F6 100%);
}
</style>
